#include "xmldocument.h"
#include "transcoder.h"

namespace MCCommon
{

static const XMLCh LS[] = { xercesc::chLatin_L, xercesc::chLatin_S, xercesc::chNull };

XMLDocument::XMLDocument
(
    xercesc::DOMDocument* pDomDoc,
    xercesc::DOMLSParser* pDomParser/*=0*/
)
:   fDomParser( pDomParser ),
    fDomDocument( pDomDoc )
{
    fAccCount.Increase();

    if ( pDomDoc )
    {
        fDomElement = pDomDoc->getDocumentElement();
    }    
}

XMLDocument::XMLDocument(const XMLDocument& document)
:   fDomDocument( document.fDomDocument ),
    fDomParser( document.fDomParser ),
    fAccCount( document.fAccCount )
{    
    fDomElement = document.fDomElement;

    fAccCount.Increase();
}

XMLDocument& 
XMLDocument::operator = (const XMLDocument& document)
{
    if (fAccCount.GetCount() == 1) 
    {
        Release();
    }
    fAccCount.Decrease();
    fDomElement = document.fDomElement;
    fDomDocument = document.fDomDocument;
    fDomParser = document.fDomParser;
    fAccCount = document.fAccCount;	
    fAccCount.Increase();

    return *this;
}

XMLDocument::~XMLDocument()
{
    if (fAccCount.GetCount() == 1) 
    {
        Release();		
    }
    fAccCount.Decrease();
}  

void XMLDocument::StartUp()
{
    xercesc::XMLPlatformUtils::Initialize();      
}

void XMLDocument::ShutDown()
{
    xercesc::XMLPlatformUtils::Terminate();
}

XMLDocument 
XMLDocument::LoadFromFile
(
    const std::string& fileName
)
{
    xercesc::DOMDocument* doc = 0;
    xercesc::DOMLSParser* pDomParser = 0;
    
    xercesc::DOMImplementationLS *impl = (xercesc::DOMImplementationLS*)xercesc::DOMImplementationRegistry::getDOMImplementation(W("LS"));
    if( impl != 0)
    {
        pDomParser = impl->createLSParser( 
            xercesc::DOMImplementationLS::MODE_SYNCHRONOUS, 0 );
          
        doc = pDomParser->parseURI( W(fileName) );
    }

    return XMLDocument( doc, pDomParser );
}

XMLDocument 
XMLDocument::LoadFromString
(
    const std::string& str
)
{
    xercesc::DOMDocument* doc = 0;
    xercesc::DOMLSParser* pDomParser = 0;

    xercesc::DOMImplementationLS *impl = (xercesc::DOMImplementationLS*)xercesc::DOMImplementationRegistry::getDOMImplementation(W("LS"));
    if(impl != 0)
    {
        pDomParser = impl->createLSParser(
            xercesc::DOMImplementationLS::MODE_SYNCHRONOUS, 0 );

        xercesc::DOMLSInput* inputs = impl->createLSInput();
        inputs->setStringData( W(str) );         
        doc = pDomParser->parse(inputs);        
        inputs->release();       
    }

    return XMLDocument(doc,pDomParser);
}

XMLDocument 
XMLDocument::CreateRootElement
(
    const std::string& rootEleName
)
{
    xercesc::DOMDocument* doc = 0;

    xercesc::DOMImplementation* impl = xercesc::DOMImplementationRegistry::getDOMImplementation(W("Core"));
    if(impl != 0)
    {
        doc = impl->createDocument( NULL, W(rootEleName), NULL);		
    }

    return XMLDocument(doc);
}

void XMLDocument::Release()
{ 
    if(fDomParser!= 0)
    {
        fDomParser->release();
    }
    else if(fDomDocument != 0)
    {   
        fDomDocument->release();       
    }
}

} // end of namespace MCXML_NAMESPACE.